package service;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import java.util.*;
import domain.Cliente;

public class SimpleClienteManager implements ClienteManager {
  private List<Cliente> elements;

  public void setClientes(List<Cliente> elements){this.elements=elements;}
  public List<Cliente> getClientes(){return this.elements;}

  public List<Cliente> getAll() {return elements;}  

  public Cliente getById(String id) {
    Iterator itr = elements.iterator();
    Cliente element;
    while (itr.hasNext()) {
      element = (Cliente)itr.next();
      if (element.getPk_cliente().equals(id)) return element;
    }
    return null;
  }
  
  public void delete(String id){

    Iterator itr = elements.iterator();

    while (itr.hasNext()) {
      Cliente element;
      element = (Cliente)itr.next();
      if (element.getPk_cliente().equals(id)){
        itr.remove();
  	  }
    }
  }
  
  public void insert(Cliente myObj){

    if (myObj.getNombre()!=null){
      Random generator = new Random();
      int id = generator.nextInt();
	    id = Math.abs(id);
		  myObj.setPk_cliente(String.valueOf(id));
		  elements.add(myObj);
    }
  }
}